﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ApplProtocol
{
    public enum ApplProtocolType
    {
        heartBeat,

        regist, registFail, registSuccess,
        login, loginFail, loginSuccess,

        roomList, createRoom, enterRoom, quitRoom, setReady, beginGame,

        position, fire, flip, damage, winner, exit
    }

    public class PclObject
    {
        public static bool handled = true;
        public static string type = "";
        public static List<string> args = new List<string>();
    }

    class PclUtils
    {
        private static Dictionary<string, Client.State> pclMatch = new Dictionary<string, Client.State>
        {
            {"registFail", Client.State.connected},
            {"registSuccess", Client.State.connected},

            {"loginFail", Client.State.connected},
            {"loginSuccess", Client.State.connected},

            {"roomList", Client.State.login},
            {"beginGame", Client.State.login},

            {"position", Client.State.inGame},
            {"fire", Client.State.inGame},
            {"flip", Client.State.inGame},
            {"health", Client.State.inGame}
        };

        static string separator = " ";
        public static string Separator
        {
            get { return separator; }
        }

        public static string Load(ApplProtocolType type, params string[] args)
        {
            string pclStr = type.ToString() + separator;

            for(int i = 0; i < args.Length; i++)
            {
                pclStr += args[i] + separator;
            }

            return pclStr;
        }

        public static bool Unload(string pclStr)
        {
            if (PclObject.handled)
            {
                string[] args = pclStr.Split(separator[0]);

                //过滤协议包
                if (pclMatch.ContainsKey(args[0]) && pclMatch[args[0]] != Client.state)
                {
                    return true;
                }

                PclObject.handled = false;
                PclObject.type = args[0];

                PclObject.args.Clear();
                for (int i = 1; i < args.Length; i++) PclObject.args.Add(args[i]);
                return true;
            }

            return false;
        }

    }


}